package com.open.capacity.license;

import com.open.capacity.utils.encrypt.DESHelper;
import com.open.capacity.utils.file.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * License校验类
 *
 * @author zifangsky
 * @date 2018/4/20
 * @since 1.0.0
 */
public class LicenseVerify {
    private static Logger logger = LogManager.getLogger(LicenseVerify.class);

    /**
     * 校验License证书
     *
     * @return boolean
     * @author zifangsky
     * @date 2018/4/20 16:26
     * @since 1.0.0
     */
    public boolean verify(String licensePath) {
        try {
            //读取授权码
            String license = FileUtils.readLineContent(licensePath, 0);

            //操作系统类型
            String osName = System.getProperty("os.name");
            osName = osName.toLowerCase();

            AbstractServerInfos abstractServerInfos = null;

            //根据不同操作系统类型选择不同的数据获取方法
            if (osName.startsWith("windows")) {
                abstractServerInfos = new WindowsServerInfos();
            } else if (osName.startsWith("linux")) {
                abstractServerInfos = new LinuxServerInfos();
            } else {//其他服务器类型
                abstractServerInfos = new LinuxServerInfos();
            }
            LicenseCheckModel data = abstractServerInfos.getServerInfos();
            String machineCode = data.getCpuSerial();

            if (license.equals(DESHelper.encrypt(machineCode))) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            logger.error("授权码校验失败！", e);
            return false;
        }
    }
}
